<?php
/**
 * 用户账户数据查询
 */
namespace app\common\query;

use app\common\model\MemberCoin as MemberCoinModel;

use app\common\cache\CacheData;

class MemberCoin
{
	private $error = '';
	static $change=[
			'dec' => '减少',
			'inc' => '增加',
		];

	public function getError(){
		return $this->error;
	}

	public static function change($key=null){
		if(!is_null($key)){
			return self::$change[$key];
		}else{
			return self::$change;
		}
	}

	/**
	 * 查询用户货币账户
	 * @return [type] [description]
	 */
	public function coinAccount($mid,$coinId=null){
		$model = new MemberCoinModel;
		if(is_null($coinId)){
			$where = ['mid'=>$mid];
			$account = $model->where($where)->select()->toArray();
			foreach($account as &$a):
				$a = $this->setRemark($a);
			endforeach;
		}else{
			$where = ['mid'=>$mid,'coin_id'=>$coinId];
			$account = $model->where($where)->find()->toArray();
			$account = $this->setRemark($account);
		}
		return $account;
	}

	/**
	 * 获取单条记录
	 * @param  [type] $where [description]
	 * @return [type]        [description]
	 */
	public function getOneData($mid,$coin_id=null){
		$where = ['mid'=>$mid];

		$model = new MemberCoinModel;
		if(!is_null($coin_id)){
			$where['coin_id'] = $coin_id;
			$data = $model->where($where)->find();
			if(!$data){
				$this->error = '数据不存在';
				return false;
			}
			$data = $data->toArray();
			$data = $this->setRemark($data);
		}else{
			$datas = $model->where($where)->select();
			if(!$datas){
				$this->error = '数据不存在';
				return false;
			}
			$datas = $datas->toArray();
			$data = [];
			foreach($datas as $value):
				$data[] = $this->setRemark($value);
			endforeach;
		}
		return $data;
	}
	/**
	 * 获取单个字段
	 * @param  [type] $field [description]
	 * @param  [type] $where [description]
	 * @return [type]        [description]
	 */
	public function getOneValue($field,$where){
    	$model = new MemberCoinModel;
    	return $model->where($where)->value($field);
    }
    /**
     * 保存单条数据
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
	public function saveOneData($data){
		$model = new MemberCoinModel;
		if($this->isExist($data['mid'],$data['coin_id'])){
			$model->where(['mid'=>$data['mid'],'coin_id'=>$data['coin_id']])->update(['amount'=>$data['amount']]);
		}else{
			$model->save($data);
		}
	}
	/**
	 * 保存更新多条数据
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function saveDatas($data){
		foreach($data as $value):
			$this->saveOneData($value);
		endforeach;
	}
	/**
	 * 判断是否存在记录
	 * @param  [type]  $mid     [description]
	 * @param  [type]  $coin_id [description]
	 * @return boolean          [description]
	 */
	public function isExist($mid,$coin_id){
		if($this->getOneData($mid,$coin_id)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 判断用户金币是否充足
	 */
	public function isHigh($data=[]){
		//dump($data);die;
		if($this->getOneValue('id',$data)){
			return false;
		}else{
			$row = self::setRemark($data);
			$title = $row['coin_name'].'货币不足';
			return ['data'=>$title];
		}
	}

	private function setRemark($row){
		if(isset($row['coin_id'])){
			$row['coin_name'] = Coin::coinData($row['coin_id'])['coin_name'];
		}
		return $row;
	}

	/**
	 * 增减用户货币
	 * @return [type] [description]
	 */
	public function changeCoin($type,$mid,$coin_id,$change_amount,$admin_id){

		$model = new MemberCoinModel;

		$model -> startTrans();
		try{
			$where = ['mid'=>$mid,'coin_id'=>$coin_id];
			$beforeAmount = $model->where($where)->value('amount');
			if($type=='inc'){
				$model -> where($where) ->setInc('amount',$change_amount);
			}else{
				$model -> where($where) ->setDec('amount',$change_amount);
			}
			$afterAmount = $model->where($where)->value('amount');
			// 资金变动记录
			$record = [
					'operate_id' => 0,
					'mid' => $mid,
					'coin_id' => $coin_id,
					'amount' => $change_amount,
					'operate' => 'change',
					'before_amount' => $beforeAmount,
					'after_amount' => $afterAmount,
					'message' => '',
					'remark' => '管理员'.$admin_id.'增减金额',
					'flag' => $type
				];
			$memberCoinRecordModel = new \app\common\model\MemberCoinRecord;
			$memberCoinRecordModel -> allowField(true) -> save($record);

			$model -> commit();
		}catch(Exception $e){
			$model -> rollback();
			$this->error = $e->getMessage();
			return false;
		}
		return true;
	}

	/**
	 * 给所有会员新增一种货币账户
	 */
	public function addNewCoinAccount($coin_id){
		$memberModel = new \app\common\model\Member;
		$mids = $memberModel->field('mid')->select()->toArray();
		$datas = [];
		foreach($mids as $mid):
			$datas[] = [
				'mid'=>$mid['mid'],
				'coin_id'=>$coin_id,
				'amount'=>0,
			];
		endforeach;

		$model = new MemberCoinModel;
		$model->saveAll($datas);
	}

}